﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Raider.WinForms.Actions.Standard;

namespace Raider.WinForms.Actions.Design
{
    public partial class StandardActionDialog : Form
    {
        private readonly static Type[] standardActions;

        static StandardActionDialog()
        {
            Type standardActionType = typeof(StandardAction);
            standardActions = standardActionType.Assembly.GetTypes()
                .Where(type => type.IsSubclassOf(standardActionType) && !type.ContainsGenericParameters && !type.IsAbstract)
                .ToArray();
        }

        public StandardActionDialog()
        {
            InitializeComponent();
            treeView.NoneImageIndex = 1;
        }

        private void StandardActionDialogLoad(object sender, EventArgs e)
        {
            Font = SystemFonts.MessageBoxFont;

            // Fill tree view.
            treeView.BeginUpdate();
            foreach (Type action in standardActions)
            {
                var actionInstance = (StandardAction)Activator.CreateInstance(action);
                var description = Attribute.GetCustomAttribute(action, typeof(DescriptionAttribute));
                var category = actionInstance.Category;
                var icon = actionInstance.Image;

                if (category != null && description != null)
                {
                    TreeNode node = !treeView.Nodes.ContainsKey(category) 
                                        ? treeView.Nodes.Add(category, category) 
                                        : treeView.Nodes[category];

                    int imageIndex = icon != null
                                         ? imageList.Images.Add(icon, Color.Transparent)
                                         : treeView.NoneImageIndex;

                    TreeNode subNode = node.Nodes.Add(null, action.Name, imageIndex, imageIndex);
                    subNode.ToolTipText = ((DescriptionAttribute)description).Description;
                    subNode.Tag = action;
                }

                actionInstance.Dispose();
            }
            treeView.EndUpdate();

            // Expand tree view.
            treeView.ExpandAll();
        }

        private void TreeViewAfterSelect(object sender, TreeViewEventArgs e)
        {
            okButton.Enabled = e.Node != null;
        }

        private void TreeViewNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null && e.Node.Tag != null)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        public Type[] GetSelectedActionTypes()
        {
            var list = new List<Type>();
            if (treeView.SelectedNode != null)
            {
                if (treeView.SelectedNode.Tag == null)
                {
                    list.AddRange(
                        from TreeNode subNode 
                        in treeView.SelectedNode.Nodes
                        select (Type)subNode.Tag);
                }
                else
                {
                    list.Add((Type)treeView.SelectedNode.Tag);
                }
            }

            return list.ToArray();
        }
    }
}
